############################################
############################################
############################################
####R version 3.2.1########################
####ZeligMultilevel version 0.7.1 was downloaded from
####https://cran.r-project.org/src/contrib/Archive/ZeligMultilevel/
####and installed using the downloaded local source package


library(foreign)
library(Zelig)#version 4.2.1
library(ZeligMultilevel)#version 0.7.1
library(lme4)#version 1.1.12
library(sampleSelection)#version 1.0.12
library(robustbase)#version 0.92.6
library(texreg)

############################################
#####OPENING DATASETS#######################
setwd("/Users/raimondasibenskas/Google Drive/Gothenburg/ECPRGeneral/RR2/replication")

rump<-read.csv("rump.csv")
splinter<-read.csv("splinter.csv")
rumpsplit<-read.csv("rumpsplit.csv")
parties<-read.csv("parties.csv")

###Subset of "splinter" dataset with non-missing values
##of an alternative measure of the membership organization of the splinter party
splinter1<-splinter[is.na(splinter$log_me_splinter)=="FALSE",]
############################################
############################################


################################################################################################
#Figure 1
png(filename="Figure1.png",width=11,height=5,res=350,units="in")
par(mfrow=c(1,3))
#- rump
rump$col<-rump$cee
rump$col[rump$cee==0]<-"black"
rump$col[rump$cee==1]<-"darkgray"
plot(rump$rumpvote~rump$vote,main="Rump party vote",
xlab="Parent party vote",ylab="Rump party vote",
pch=16,ylim=c(0,50),xlim=c(0,68),col=rump$col,cex=1.4)
abline(0,1)
#- splinter
splinter$col<-splinter$cee
splinter$col[splinter$cee==0]<-"black"
splinter$col[splinter$cee==1]<-"darkgray"
plot(splinter$splitvote~splinter$vote,main="Splinter party vote",
xlab="Parent party vote",ylab="Splinter party vote",
pch=16,ylim=c(0,50),xlim=c(0,68),col=splinter$col,cex=1.4)
abline(0,1)
#- residual+splinter
rumpsplit$col<-rumpsplit$cee
rumpsplit$col[rumpsplit$cee==0]<-"black"
rumpsplit$col[rumpsplit$cee==1]<-"darkgray"
plot(rumpsplit$rumpsplitvote~rumpsplit$vote,main="Rump and splinter party vote",
xlab="Parent party vote",ylab="Successor party vote",
pch=16,ylim=c(0,50),xlim=c(0,68),col=rumpsplit$col,cex=1.4)
abline(0,1)
dev.off()
#################################################################################################

############################################
############################################
#Main analyses (Table 1)
##Model 1
m1<-lmer(log_rumpvote~log_sharelefttot+log_me+comsoc+log_age+gdp_growth*govbef++
           log_maxeffleg+fund_onlyparl_next+enep+zeroissues+ideoldiff+splitgov+multsplit+
           log_vote+(1|parties)+(1|el_index)+(1|country),data=rump)

###Model 2
m2<-update(m1,log_splitvote ~ .,data=splinter)

###Model 2.1 (a different measure of the membership organization of the splinter party)
splinter1<-splinter[is.na(splinter$log_me_splinter)=="FALSE",]
m21<-update(m1,log_splitvote ~ .+log_me_splinter-log_me,data=splinter1)

#Model3
m3<-update(m1,log_rumpsplitvote ~ .,data=rumpsplit)
############################################
############################################

############################################
############################################
#Robust regression models
m4<-lmrob(rumpvote~sharelefttot+me+comsoc+age+gdp_growth*govbef+
          maxeffleg+fund_onlyparl_next+enep+zeroissues+ideoldiff+splitgov+multsplit+
          vote,setting="KS2011",method="MM",
          data=rump)
m5<-lmrob(splitvote~sharelefttot+me+comsoc+age+gdp_growth*govbef+
          maxeffleg+fund_onlyparl_next+enep+zeroissues+ideoldiff+splitgov+multsplit+
          vote,setting="KS2011",method="MM",
          data=splinter)
m51<-update(m5,~ .+me_splinter - me,data=splinter1)
m6<-lmrob(rumpsplitvote~sharelefttot+me+comsoc+age+gdp_growth*govbef+
          maxeffleg+fund_onlyparl_next+enep+zeroissues+ideoldiff+splitgov+multsplit+
          vote,setting="KS2011",method="MM",
          data=rumpsplit)
############################################
############################################


############################################
#####Export results to Latex
texreg(list(m1,m2,m21,m3,m4,m5,m51,m6),
caption="Comparative analysis of party splits",
dcolumn=FALSE,
custom.model.names=c("Rump","Splinter","Splinter", "Rump+splinter","Rump","Splinter",
                     "Splinter","Rump+splinter"),
custom.coef.names=c("Intercept",
                    "Share of splinter MPs",
                    "Member-electorate ratio (parent party)",
                    "Socdem or communist",
                    "Party age",
                    "Economic growth",
                    "Rump in government",
                    "Effective threshold",
                    "Direct public funding for leg. parties only",
                    "Effective number of parties",
                    "No of zero categories",
                    "Different ideologies",
                    "Splinter in government",
                    "Multiple splits",
                    "Previous vote",
                    "Rump in gov * economic growth",
                    "Member-electorate ratio (splinter party)",
                    "Share of splinter MPs",
                    "Member-electorate ratio (parent party)",
                    "Party age",
                    "Effective threshold",
                    "Previous vote",
                    "Member-electorate ratio (splinter party)"),
stars=c(0.05))
############################################

############################################
############################################
############Figure 2#######################

###########Zelig models#####################
z1<-zelig(log_rumpvote~log_sharelefttot+log_me+comsoc+log_age+
            log_maxeffleg++gdp_growth*govbef+ideoldiff+
            splitgov+enep+zeroissues+multsplit+
            log_vote+tag(1|parties)+tag(1|el_index)+tag(1|country),model="ls.mixed",
            data=rump)
z2<-zelig(log_splitvote~log_sharelefttot+log_me+comsoc+log_age+
            log_maxeffleg++gdp_growth*govbef+ideoldiff+
            splitgov+enep+zeroissues+multsplit+log_vote+
            tag(1|parties)+tag(1|el_index)+tag(1|country),model="ls.mixed",
            data=splinter)
z3<-zelig(log_rumpsplitvote~log_sharelefttot+log_me+comsoc+log_age+
            log_maxeffleg++gdp_growth*govbef+ideoldiff+
            splitgov+enep+zeroissues+multsplit+log_vote+
            tag(1|parties)+tag(1|el_index)+tag(1|country),model="ls.mixed",
            data=rumpsplit)
z4<-zelig(log_splitvote~log_sharelefttot+log_me_splinter+comsoc+
            log_age+log_maxeffleg++gdp_growth*govbef+ideoldiff+
            splitgov+zeroissues+
            multsplit+log_vote+tag(1|parties)+tag(1|el_index)+tag(1|country),model="ls.mixed",
            data=splinter1)
############################################

############################################
######Setting the values of predictor variables#####
x1<-setx(z1,log_sharelefttot=log(seq(0,7,0.005)+0.5),log_me=mean(rump$log_me),
            comsoc=0,log_age=mean(rump$log_age),
            log_maxeffleg=mean(rump$log_maxeffleg),
            gdp_growth=mean(rump$gdp_growth),
            govbef=0,splitgov=0,enep=mean(rump$enep),
            zeroissues=mean(rump$zeroissues),ideoldiff=0,
            log_vote=mean(rump$log_vote),multsplit=0)

x2<-setx(z1,log_me=log(seq(0.04,5,0.005)),
            log_sharelefttot=mean(rump$log_sharelefttot),
            comsoc=0,log_age=mean(rump$log_age),
            log_maxeffleg=mean(rump$log_maxeffleg),
            gdp_growth=mean(rump$gdp_growth),
            govbef=0,splitgov=0,enep=mean(rump$enep),
            zeroissues=mean(rump$zeroissues),ideoldiff=0,
            log_vote=mean(rump$log_vote),multsplit=0)

meanlogme<-mean(splinter$log_me)
meanlogage<-mean(splinter$log_age)
meanlogmaxeffleg<-mean(splinter$log_maxeffleg)
meangdpgrowth<-mean(splinter$gdp_growth)
meanenep<-mean(splinter$enep)
meanzeroissues<-mean(splinter$zeroissues)
meanlogvote<-mean(splinter$log_vote)
meanlogsharelefttot<-mean(splinter$log_sharelefttot)

x3<-setx(z2,log_sharelefttot=log(seq(0,7,0.005)+0.5),
            log_me=meanlogme,comsoc=0,
            log_age=meanlogage,
            log_maxeffleg=meanlogmaxeffleg,
            gdp_growth=meangdpgrowth,govbef=0,
            splitgov=0,enep=meanenep,
            zeroissues=meanzeroissues,ideoldiff=0,
            log_vote=meanlogvote,multsplit=0)

x4<-setx(z4,log_sharelefttot=mean(splinter1$log_sharelefttot),
            log_me_splinter=log(seq(0.002,0.5,0.0001)),comsoc=0,
            log_age=mean(splinter1$log_age),
            log_maxeffleg=mean(splinter1$log_maxeffleg),
            gdp_growth=mean(splinter1$gdp_growth),ideoldiff=0,
            govbef=0,splitgov=0,zeroissues=mean(splinter1$zeroissues),
            log_vote=mean(splinter1$log_vote),multsplit=0)
############################################

############################################
##Function to summarise simulation results##
resultsim<-function(simul,x){
	lsim<-length(simul)
	medsim<-rep(NA,lsim)
	lbsim<-rep(NA,lsim)
	hbsim<-rep(NA,lsim)
	for (i in 1:lsim){
		medsim[i]<-exp(median(simul[[i]]$qi[[1]])-0.5)
		lbsim[i]<-exp(quantile(simul[[i]]$qi[[1]],0.025)-0.5)
		hbsim[i]<-exp(quantile(simul[[i]]$qi[[1]],0.975)-0.5)		
	}
	dat<-data.frame(x,medsim,lbsim,hbsim)
	return(dat)	
}
############################################

##############Simulations##################
result1<-resultsim(sim(z1,x=x1),seq(0,7,0.005))
result2<-resultsim(sim(z1,x=x2),seq(0.04,5,0.005))
result3<-resultsim(sim(z2,x=x3),seq(0,7,0.005))
result4<-resultsim(sim(z4,x=x4),seq(0.002,0.5,0.0001))
############################################

################Effect plots################
png("Figure2.png",width=12,height=10,res=350,units="in")
par(mfrow=c(2,2))
plot(lowess(result1$x,result1$medsim,f=0.02),type="l",ylim=c(5,12),
     xlab="Share of splinter MPs",ylab="Vote share of the rump party",
     main="Share of splinter MPs and the support of the rump party (Model 1)",cex=1.5)
lines(lowess(result1$x,result1$lbsim,f=0.02),type="l",lty=2)
lines(lowess(result1$x,result1$hbsim,f=0.02),type="l",lty=2)

plot(lowess(result3$x,result3$medsim,f=0.02),type="l",ylim=c(0,3),
     xlab="Share of splinter MPs",ylab="Vote share of the splinter party",
     main="Share of splinter MPs and the support of the splinter party (Model 2)",cex=1.5)
lines(lowess(result3$x,result3$lbsim,f=0.02),type="l",lty=2)
lines(lowess(result3$x,result3$hbsim,f=0.02),type="l",lty=2)

plot(lowess(result2$x,result2$medsim,f=0.02),type="l",ylim=c(5,12),
     xlab="Member-electorate ratio (parent party)",ylab="Vote share of the rump party",
     main="Membership strength and the support of the rump party (Model 1)",cex=1.5)
lines(lowess(result2$x,result2$lbsim,f=0.02),type="l",lty=2)
lines(lowess(result2$x,result2$hbsim,f=0.02),type="l",lty=2)

plot(lowess(result4$x,result4$medsim,f=0.02),type="l",ylim=c(0,3),
     xlab="Member-electorate ratio (splinter) party)",ylab="Vote share of the splinter party",
     main="Membership strength and the support of the splinter party (Model 3)",cex=1.5)
lines(lowess(result4$x,result4$lbsim,f=0.02),type="l",lty=2)
lines(lowess(result4$x,result4$hbsim,f=0.02),type="l",lty=2)
dev.off()
############################################
############################################

############################################
############################################

############################################
############################################
#Separate analyses for Western and Eastern Europe

#Creating subset datasets
#West
rumpwest<-rump[rump$cee==0,]
splinterwest<-splinter[splinter$cee==0,]
rumpsplitwest<-rumpsplit[rumpsplit$cee==0,]
splinter1west<-splinter1[splinter1$cee==0,]

#East
rumpeast<-rump[rump$cee==1,]
splintereast<-splinter[splinter$cee==1,]
rumpspliteast<-rumpsplit[rumpsplit$cee==1,]
splinter1east<-splinter1[splinter1$cee==1,]

#Models for Western Europe (Table 2)
m1w<-update(m1,.~.,data=rumpwest)
m2w<-update(m2,.~.,data=splinterwest)
m21w<-update(m21,.~.,data=splinter1west)
m3w<-update(m3,.~.,data=rumpsplitwest)
m4w<-update(m4,.~.,data=rumpwest)
m5w<-update(m5,.~.,data=splinterwest)
m51w<-update(m51,.~.,data=splinter1west)
m6w<-update(m6,.~.,data=rumpsplitwest)

texreg(list(m1w,m2w,m21w,m3w,m4w,m5w,m51w,m6w),
       caption="Comparative analysis of party splits",
       dcolumn=FALSE,
       custom.model.names=c("Rump","Splinter","Splinter", "Rump+splinter","Rump","Splinter","Splinter","Rump+splinter"),
       custom.coef.names=c("Intercept",
                           "Share of splinter MPs",
                           "Member-electorate ratio (parent party)",
                           "Socdem or communist & high trade union density",
                           "Parent party age",
                           "Economic growth",
                           "Rump in government",
                           "Effective electoral threshold",
                           "Direct public funding for leg. parties only",
                           "Effective number of parties",
                           "No of zero categories",
                           "Different party families",
                           "Splinter in government",
                           "Multiple splits",
                           "Previous vote",
                           "Rump in gov * economic growth",
                           "Member-electorate ratio (splinter party)",
                           "Share of splinter MPs",
                           "Member-electorate ratio (parent party)",
                           "Parent party age",
                           "Effective electoral threshold",
                           "Previous vote",
                           "Member-electorate ratio (splinter party)"),
       stars=c(0.05))

#Models for Eastern Europe (Table 3)
m1e<-update(m1,.~.,data=rumpeast)
m2e<-update(m2,.~.,data=splintereast)
m21e<-update(m21,.~.,data=splinter1east)
m3e<-update(m3,.~.,data=rumpspliteast)
m4e<-update(m4,.~.,data=rumpeast)
m5e<-update(m5,.~.,data=splintereast)
m51e<-update(m51,.~.,data=splinter1east)
m6e<-update(m6,.~.,data=rumpspliteast)

texreg(list(m1e,m2e,m21e,m3e,m4e,m5e,m51e,m6e),
       caption="Comparative analysis of party splits",
       dcolumn=FALSE,
       custom.model.names=c("Rump","Splinter","Splinter", "Rump+splinter","Rump","Splinter","Splinter","Rump+splinter"),
       custom.coef.names=c("Intercept",
                           "Share of splinter MPs",
                           "Member-electorate ratio (parent party)",
                           "Socdem or communist",
                           "Party age",
                           "Economic growth",
                           "Rump in government",
                           "Effective threshold",
                           "Direct public funding for leg. parties only",
                           "Effective number of parties",
                           "No of zero categories",
                           "Different party families",
                           "Splinter in government",
                           "Multiple splits",
                           "Previous vote",
                           "Rump in gov * economic growth",
                           "Member-electorate ratio (splinter party)",
                           "Share of splinter MPs",
                           "Member-electorate ratio (parent party)",
                           "Party age","Effective threshold",
                           "Previous vote",
                           "Member-electorate ratio (splinter party)"),
       stars=c(0.05))
############################################
############################################


############################################
############################################
####SAMPLE SELECTION MODELS (ONLINE APPENDIX B)
h1<-selection(splitall~government+
                log_age+log_maxeffleg+zeroissues+enep+log_petit+reg_cost,
log_rumpvote~log_sharelefttot+log_me+comsoc+log_age+gdp_growth*govbef+
                log_maxeffleg+fund_onlyparl_next+ideoldiff+
                zeroissues+enep+splitgov+multsplit+
                log_vote,data=parties,method="2step")

h2<-selection(splitall~government+
                log_age+log_maxeffleg+zeroissues+enep+log_petit+reg_cost,
log_splitvote~log_sharelefttot+log_me+comsoc+log_age+gdp_growth*govbef+
                log_maxeffleg+fund_onlyparl_next+ideoldiff+
                zeroissues+enep+splitgov+multsplit+
                log_vote,data=parties,method="2step")

h3<-selection(splitall~government+
                log_age+log_maxeffleg+zeroissues+enep+log_petit+reg_cost,
log_rumpsplitvote~log_sharelefttot+log_me+comsoc+log_age+gdp_growth*govbef+
                log_maxeffleg+fund_onlyparl_next+ideoldiff+
                zeroissues+enep+splitgov+multsplit+
                log_vote,data=parties,method="2step")
############################################
############################################

############################################
############################################
texreg(list(h1,h2,h3),
caption="Comparative analysis of party splits: Heckman's selection model",
dcolumn=FALSE,
custom.model.names=c("Rump","Splinter","Rump+splinter"),
custom.coef.names=c("Intercept",
                    "Government party",
                    "Ln (party age)",
                    "Ln (effective electoral threshold)",
                    "No of zero categories",
                    "Ln (petition)",
                    "Electoral deposit",
                    "Effective number of parties",
                    "Intercept",
                    "Ln (share of splinter MPs)",
                    "Ln (member-electorate ratio)",
                    "Socdem or communist",
                    "Ln (party age)",
                    "Economic growth",
                    "Rump party in government",
                    "Ln (effective threshold)",
                    "Public funding for leg. parties only",
                    "Effective number of parties",
                    "No of zero categories",
                    "Different ideological families",
                    "Splinter party in government",
                    "Multiple splits",
                    "Ln (previous vote)",
                    "Rump party in gov * economic growth",
                    "Inverse Mills ratio","Sigma","Rho"))
############################################
############################################
